# Load necessary packages
library(foreign)
library(manifestoR)
library(dplyr)
library(tidyr)
library(glue)
library(stringr)
library(WriteXLS)
library(readtext)
library(readxl)
library(ngram)
library(tidyverse)
library(stargazer)
library(lfe)
library(effects)
library(sjPlot)
library(prediction)
library(pastecs)
library(devtools)
library(ggplot2)
library(xtable)

# Install haschaR package from GitHub (using devtools)
devtools::install_github("hhilbig/haschaR")
library(haschaR)


# Clear the workspace
rm(list=ls())

## Set working directory
getwd()
# setwd()

## Read cabinet-level data and select necessary variables
cabinet_dat <- read_csv('dat_replication_cabinet.csv') %>%
  select(votes_lead, vote_share, votes_diff, votes_diff, vote_ratio,
         mentioned_and_fulfilled,mentioned,  mentioned_relative, growth_change, log_cab_rile_dist,
         no_cabparties, minority, p_prime, country_name_short, east,
         cabinet_name, eyear)


## LDV-Approach: Tab. A7 & Fig. 1
ldv_model_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
ldv_model_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
ldv_model_3 <- felm(votes_lead ~ mentioned_relative * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
ldv_model_4 <- felm(votes_lead ~ mentioned_and_fulfilled * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
ldv_model_5 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)
ldv_model_6 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short ,data = cabinet_dat)
ldv_model_7 <- felm(votes_lead ~ mentioned_relative * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)
ldv_model_8 <- felm(votes_lead ~ mentioned_and_fulfilled * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)

# Generate Table A7
stargazer::stargazer(ldv_model_1, ldv_model_2, ldv_model_3, ldv_model_4, ldv_model_5, ldv_model_6, ldv_model_7, ldv_model_8, type = "text",
                     omit.stat = c( "ser", "f"), add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "None", "None", "None", "None")),
                     out = c("tab_a7_ldv.tex", "tab_a7_ldv.html"))

# Generate Figure 1
plot_pooled_1 <- plot_models(ldv_model_1, ldv_model_2, ldv_model_5, ldv_model_6,
                             rm.terms = c("growth_change", "vote_share", "log_cab_rile_dist", "no_cabparties", "minority", "factor(east)1"),
                             axis.labels = c("Party Pledge Count", "Party Pledge Share"),
                             show.legend = FALSE, colors = "bw") +
  geom_hline(yintercept = 0, colour = "darkgrey", linetype = "longdash") +  theme_hanno() 

plot_pooled_2 <- plot_models(ldv_model_3, ldv_model_4, ldv_model_7, ldv_model_8,
                             rm.terms = c("growth_change", "vote_share", "log_cab_rile_dist", "no_cabparties", "minority", "factor(east)1", "p_prime", "mentioned_and_fulfilled", "mentioned_relative"),
                             axis.labels = c("Pledge Count:PM Party", "Pledge Share:PM Party"),
                             show.legend = FALSE, colors = "bw", title = "") +
  geom_hline(yintercept = 0, colour = "darkgrey", linetype = "longdash") +  theme_hanno() 

gridExtra::grid.arrange(plot_pooled_1, plot_pooled_2, ncol = 1) 
paperplot_1 <- gridExtra::arrangeGrob(plot_pooled_1, plot_pooled_2, ncol = 1) 
paperplot_1
ggsave('fig_1_LDV_Cabinets.pdf', paperplot_1, height = 5, width = 10)

## Table A13: Vote Differential
vote_diff_model_1 <- felm(votes_diff ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_diff_model_2 <- felm(votes_diff ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_diff_model_3 <- felm(votes_diff ~ mentioned_relative * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_diff_model_4 <- felm(votes_diff ~ mentioned_and_fulfilled * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_diff_model_5 <- felm(votes_diff ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)
vote_diff_model_6 <- felm(votes_diff ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short  , data = cabinet_dat)
vote_diff_model_7 <- felm(votes_diff ~ mentioned_relative  * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)
vote_diff_model_8 <- felm(votes_diff ~ mentioned_and_fulfilled * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = cabinet_dat)

# Generate Table A13
stargazer::stargazer(vote_diff_model_1, vote_diff_model_2, vote_diff_model_3, vote_diff_model_4, vote_diff_model_5, vote_diff_model_6, vote_diff_model_7, vote_diff_model_8, type = "text",
                     omit.stat = c( "ser", "f"), add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "None", "None", "None", "None")),
                     out = c("tab_A13_vote_diff.tex", "tab_A13_vote_diff.html"))

## Table A10: Vote Ratio
vote_ratio_model_1 <- felm(vote_ratio ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_2 <- felm(vote_ratio ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_3 <- felm(vote_ratio ~ mentioned_relative * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_4 <- felm(vote_ratio ~ mentioned_and_fulfilled * p_prime + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_5 <- felm(vote_ratio ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_6 <- felm(vote_ratio ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_7 <- felm(vote_ratio ~ mentioned_relative * p_prime  + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short ,data = cabinet_dat)
vote_ratio_model_8 <- felm(vote_ratio ~ mentioned_and_fulfilled * p_prime  + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short  ,data = cabinet_dat)

# Generate Table A10
stargazer::stargazer(vote_ratio_model_1, vote_ratio_model_2, vote_ratio_model_3, vote_ratio_model_4, vote_ratio_model_5, vote_ratio_model_6, vote_ratio_model_7, vote_ratio_model_8, type = "text",
                     omit.stat = c( "ser", "f"), add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "None", "None", "None", "None")),
                     out = c("tab_A10_vote_ratio.tex", "tab_A10_vote_ratio.html"))

## Replication of Tab 7 for Western Sample
west <- c("AUT", "BEL", "DEU", "DNK", "FIN", "GBR", "IRL", "ISL", "LUX", "NLD", "NOR", "SWE")
cabinet_dat_west <- cabinet_dat %>%
  filter(country_name_short %in% west)

## Tab 17: LDV West Only
ldv_west_model_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat_west)
ldv_west_model_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat_west)
ldv_west_model_3 <- felm(votes_lead ~ mentioned_relative * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat_west)
ldv_west_model_4 <- felm(votes_lead ~ mentioned_and_fulfilled * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short ,data = cabinet_dat_west)
ldv_west_model_5 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short,data = cabinet_dat_west)
ldv_west_model_6 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short ,data = cabinet_dat_west)
ldv_west_model_7 <- felm(votes_lead ~ mentioned_relative * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short,data = cabinet_dat_west)
ldv_west_model_8 <- felm(votes_lead ~ mentioned_and_fulfilled * p_prime + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short,data = cabinet_dat_west)

# Generate Table A17
stargazer::stargazer(ldv_west_model_1, ldv_west_model_2, ldv_west_model_3, ldv_west_model_4, ldv_west_model_5, ldv_west_model_6, ldv_west_model_7, ldv_west_model_8, type = "text",
                     omit.stat = c( "ser", "f"), add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "None", "None", "None", "None")),
                     out = c("tab_A17_ldv_west.tex", "tab_A17_ldv_west.html"))

## Descriptives
dat_descr <- cabinet_dat %>%
  select(votes_lead, vote_share, mentioned_and_fulfilled, mentioned_relative, growth_change, log_cab_rile_dist, no_cabparties, minority, east)

dat_descr_tab <- psych::describe(dat_descr)
dat_descr_tab$Variable <- row.names(dat_descr_tab)
dat_descr_tab <- as_tibble(dat_descr_tab) %>%
  relocate(Variable) %>% select(-vars)

# Generate Table A1

print(xtable(dat_descr_tab), type="latex", include.rownames=FALSE, 
      file = "tab_A1_descriptives_cabinets.tex" ) 


########################################################################
## Issue-Level Analysis
########################################################################
  getwd()
issues_dat <- read_csv("dat_replication_issues.csv")
# Select necessary variables
issues_dat <- select(issues_dat, mentioned_relative, 
                     vote_share, votes_lead, votes_diff,vote_ratio,
                     sal_manif, mentioned_and_fulfilled, avg_words,
                     growth_change, log_cab_rile_dist, salie,
                     no_cabparties, minority, country_name_short, country_name.x,
                     n_hard_pledge_all, fulfilled_all, mentioned_all,mentioned_and_fulfilled_all,
                     issue.x, n_hard_pledge, mentioned,
                     east)


##### Issues LDV #####
m_LDV_issue_FE_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_3 <- felm(votes_lead ~ mentioned_relative * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_4 <- felm(votes_lead ~ mentioned_and_fulfilled * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_5 <- felm(votes_lead ~ mentioned_relative * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_6 <- felm(votes_lead ~ mentioned_and_fulfilled * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_7 <- felm(votes_lead ~ mentioned_relative * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_LDV_issue_FE_8 <- felm(votes_lead ~ mentioned_and_fulfilled * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_LDV_issue_FE_1, m_LDV_issue_FE_2, m_LDV_issue_FE_3, m_LDV_issue_FE_4, m_LDV_issue_FE_5, m_LDV_issue_FE_6, m_LDV_issue_FE_7, m_LDV_issue_FE_8,
                     type = "text",  omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "Country", "Country", "Country", "Country")),
                     out = c("tab_A8_LDV_issue_FE.tex", "tab_A8_LDV_issue_FE.html"))
# No FE
m_LDV_issue_no_FE_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_3 <- felm(votes_lead ~ mentioned_relative * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_4 <- felm(votes_lead ~ mentioned_and_fulfilled * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_5 <- felm(votes_lead ~ mentioned_relative * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_6 <- felm(votes_lead ~ mentioned_and_fulfilled * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_7 <- felm(votes_lead ~ mentioned_relative * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_LDV_issue_no_FE_8 <- felm(votes_lead ~ mentioned_and_fulfilled * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_LDV_issue_no_FE_1, m_LDV_issue_no_FE_2, m_LDV_issue_no_FE_3, m_LDV_issue_no_FE_4, m_LDV_issue_no_FE_5, m_LDV_issue_no_FE_6, m_LDV_issue_no_FE_7, m_LDV_issue_no_FE_8,
                     type = "text", omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "None", "None", "None", "None", "None", "None", "None", "None")),
                     out = c("tab_A9_LDV_issue_noFE.tex", "tab_A9_LDV_issue_noFE.html"))

## Plot:
##### Figure 2: Issue-level LDV Plots #####

pm1 <- plot_models(m_LDV_issue_FE_1, m_LDV_issue_FE_2, m_LDV_issue_no_FE_1, m_LDV_issue_no_FE_2,
                   rm.terms = c("growth_change", "vote_share", "log_cab_rile_dist", "no_cabparties", "minority", "factor(east)1"),
                   axis.labels = c("Party Pledge Count", "Party Pledge Share"),
                   title = "",
                   show.legend = FALSE, colors = "bw", grid = FALSE) +
  geom_hline(yintercept = 0, colour = "darkgrey", linetype = "longdash") +
  theme_hanno()

pm1
pm2 <- plot_models(m_LDV_issue_FE_3, m_LDV_issue_FE_4, m_LDV_issue_FE_5, m_LDV_issue_FE_6, m_LDV_issue_FE_7, m_LDV_issue_FE_8, m_LDV_issue_no_FE_3, m_LDV_issue_no_FE_4, m_LDV_issue_no_FE_5, m_LDV_issue_no_FE_6, m_LDV_issue_no_FE_7, m_LDV_issue_no_FE_8,
                   rm.terms = c("growth_change", "vote_share", "log_cab_rile_dist", "no_cabparties", "minority", "factor(east)1", "p_prime", "mentioned_and_fulfilled", "mentioned_relative", "sal_manif", "avg_words", "salie"),
                   axis.labels = c("Pledge Count:Agreement Salience", "Pledge Share:Agreement Salience", "Pledge Count:Pledge Salience", "Pledge Share:Pledge Salience", "Pledge Count:Manifesto Salience", "Pledge Share:Manifesto Salience"),
                   title = "",
                   show.legend = FALSE, colors = "bw", grid = FALSE) +
  ylim(-0.25, 0.25) +
  geom_hline(yintercept = 0, colour = "darkgrey", linetype = "longdash") +
  theme_hanno()
pm2
paperplot_2 <- gridExtra::arrangeGrob(pm1, pm2, ncol = 1)
ggsave('Fig_2_LDV_issue.pdf', paperplot_2, height = 7, width = 10)



##### Issue Vote Ratio #####
m_vote_ratio_FE_1 <- felm(vote_ratio ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_2 <- felm(vote_ratio ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_3 <- felm(vote_ratio ~ mentioned_relative * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_4 <- felm(vote_ratio ~ mentioned_and_fulfilled * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_5 <- felm(vote_ratio ~ mentioned_relative * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_6 <- felm(vote_ratio ~ mentioned_and_fulfilled * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_7 <- felm(vote_ratio ~ mentioned_relative * salie + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_vote_ratio_FE_8 <- felm(vote_ratio ~ mentioned_and_fulfilled * salie + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_vote_ratio_FE_1, m_vote_ratio_FE_2, m_vote_ratio_FE_3, m_vote_ratio_FE_4, m_vote_ratio_FE_5, m_vote_ratio_FE_6, m_vote_ratio_FE_7, m_vote_ratio_FE_8,
                     type = "text", omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "Country", "Country", "Country", "Country")),
                     out = c("tab_A11_vote_ratio_issue_FE.tex", "tab_A11_vote_ratio_issue_FE.html"))
# No FE
m_vote_ratio_no_FE_1 <- felm(vote_ratio ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_2 <- felm(vote_ratio ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_3 <- felm(vote_ratio ~ mentioned_relative * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_4 <- felm(vote_ratio ~ mentioned_and_fulfilled * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_5 <- felm(vote_ratio ~ mentioned_relative * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_6 <- felm(vote_ratio ~ mentioned_and_fulfilled * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_7 <- felm(vote_ratio ~ mentioned_relative * salie + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_vote_ratio_no_FE_8 <- felm(vote_ratio ~ mentioned_and_fulfilled * salie + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_vote_ratio_no_FE_1, m_vote_ratio_no_FE_2, m_vote_ratio_no_FE_3, m_vote_ratio_no_FE_4, m_vote_ratio_no_FE_5, m_vote_ratio_no_FE_6, m_vote_ratio_no_FE_7, m_vote_ratio_no_FE_8,
                     type = "text", omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "None", "None", "None", "None", "None", "None", "None", "None")),
                     out = c("tab_A12_vote_ratio_issue_noFE.tex", "tab_A12_vote_ratio_issue_noFE.html"))


##### Issue Vote Diff #####
# Country Fe :
m_issue_FE_1 <- felm(votes_diff ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_2 <- felm(votes_diff ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_3 <- felm(votes_diff ~ mentioned_relative * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_4 <- felm(votes_diff ~ mentioned_and_fulfilled * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_5 <- felm(votes_diff ~ mentioned_relative * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_6 <- felm(votes_diff ~ mentioned_and_fulfilled * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
m_issue_FE_7 <- felm(votes_diff ~ mentioned_relative * salie + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat) # manifesto salience
m_issue_FE_8 <- felm(votes_diff ~ mentioned_and_fulfilled * salie + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_issue_FE_1, m_issue_FE_2, m_issue_FE_3, m_issue_FE_4, m_issue_FE_5, m_issue_FE_6, m_issue_FE_7, m_issue_FE_8,
                     type = "text",  omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "Country", "Country", "Country", "Country")),
                     out = c("tab_A14_vote_diff_issue_FE.tex", "tab_A14_vote_diff_issue_FE.html"))
# No FE
m_issue_no_FE_1 <- felm(votes_diff ~ mentioned_relative + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_2 <- felm(votes_diff ~ mentioned_and_fulfilled + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_3 <- felm(votes_diff ~ mentioned_relative * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_4 <- felm(votes_diff ~ mentioned_and_fulfilled * sal_manif + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_5 <- felm(votes_diff ~ mentioned_relative * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_6 <- felm(votes_diff ~ mentioned_and_fulfilled * avg_words + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
m_issue_no_FE_7 <- felm(votes_diff ~ mentioned_relative * salie + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat) # manifesto salience
m_issue_no_FE_8 <- felm(votes_diff ~ mentioned_and_fulfilled * salie + growth_change + log_cab_rile_dist + no_cabparties + minority + factor(east) | 0 | 0 | country_name_short, data = issues_dat)
stargazer::stargazer(m_issue_no_FE_1, m_issue_no_FE_2, m_issue_no_FE_3, m_issue_no_FE_4, m_issue_no_FE_5, m_issue_no_FE_6, m_issue_no_FE_7, m_issue_no_FE_8,
                     type = "text", omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "None", "None", "None", "None", "None", "None", "None", "None")),
                     out = c("tab_A15_vote_diff_issue_noFE.tex", "tab_A15_vote_diff_issue_noFE.html"))




### Tab A18,A19: Replicate West Only
issues_dat_west <- issues_dat %>% filter(east == 0)

m_LDV_issue_FE_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_3 <- felm(votes_lead ~ mentioned_relative * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_4 <- felm(votes_lead ~ mentioned_and_fulfilled * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_5 <- felm(votes_lead ~ mentioned_relative * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_6 <- felm(votes_lead ~ mentioned_and_fulfilled * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_7 <- felm(votes_lead ~ mentioned_relative * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_FE_8 <- felm(votes_lead ~ mentioned_and_fulfilled * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | country_name_short | 0 | country_name_short, data = issues_dat_west)

stargazer::stargazer(m_LDV_issue_FE_1, m_LDV_issue_FE_2, m_LDV_issue_FE_3, m_LDV_issue_FE_4, m_LDV_issue_FE_5, m_LDV_issue_FE_6, m_LDV_issue_FE_7, m_LDV_issue_FE_8,
                     type = "text",
                     omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "Country", "Country", "Country", "Country", "Country", "Country", "Country", "Country")),
                     out = c("tab_A18_ldv_issue_west_FE.tex", "tab_A18_ldv_issue_west_FE.html"))

m_LDV_issue_no_FE_1 <- felm(votes_lead ~ mentioned_relative + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_2 <- felm(votes_lead ~ mentioned_and_fulfilled + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_3 <- felm(votes_lead ~ mentioned_relative * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_4 <- felm(votes_lead ~ mentioned_and_fulfilled * sal_manif + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_5 <- felm(votes_lead ~ mentioned_relative * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_6 <- felm(votes_lead ~ mentioned_and_fulfilled * avg_words + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_7 <- felm(votes_lead ~ mentioned_relative * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
m_LDV_issue_no_FE_8 <- felm(votes_lead ~ mentioned_and_fulfilled * salie + vote_share + growth_change + log_cab_rile_dist + no_cabparties + minority | 0 | 0 | country_name_short, data = issues_dat_west)
stargazer::stargazer(m_LDV_issue_no_FE_1, m_LDV_issue_no_FE_2, m_LDV_issue_no_FE_3, m_LDV_issue_no_FE_4, m_LDV_issue_no_FE_5, m_LDV_issue_no_FE_6, m_LDV_issue_no_FE_7, m_LDV_issue_no_FE_8,
                     type = "text",
                     omit.stat = c( "ser", "f"),
                     add.lines = list(c("Fixed Effects", "None", "None", "None", "None", "None", "None", "None", "None")),
                     out = c("tab_A19_ldv_issue_west_noFE.tex", "tab_A19_ldv_issue_west_noFE.html"))

################################
### Issue level Descriptives:
################################

# Table A2
dat_descr <- issues_dat %>% select(
  votes_lead, vote_share, mentioned_and_fulfilled, mentioned_relative,
  avg_words, salie, sal_manif,  growth_change, log_cab_rile_dist,
  no_cabparties, minority )

dat_descr_tab <- psych::describe(dat_descr)
dat_descr_tab$Variable <- row.names(dat_descr_tab) 
dat_descr_tab <- as_tibble(dat_descr_tab) %>% relocate(Variable)
dat_descr_tab <- dat_descr_tab  %>% dplyr::select(-vars, -trimmed, -skew, -kurtosis, -se, - mad)
print(xtable(dat_descr_tab), type="latex", include.rownames=FALSE, 
      file = "tab_A2_descript_issues.tex" ) 


## Tab. A3 (Country Overview):
countries <- cabinet_dat %>% 
  group_by(
    country_name_short  ) %>% 
  summarise(
    # cabinet_name = first(cabinet_name),
    eyear_min = min(eyear),
    eyear_max = max(eyear)  ) %>% 
  drop_na() %>%   distinct()
print(xtable(countries), type="latex", include.rownames=FALSE, 
      file = "tab_A3_countries.tex" ) 


## Tab A4 (Cabinets Overview):
cabinets <- cabinet_dat %>% group_by( cabinet_name, eyear) %>% 
  summarise(
    cabinet_name = first(cabinet_name),
    eyear = first(eyear),
    parties = n(),
    mentioned = sum(mentioned, na.rm = T)
  ) %>% drop_na() %>%   distinct()
print(xtable(cabinets), type="latex", include.rownames=FALSE, 
      file = "tab_A4_A5_cabinets.tex" ) 

#### #### #### #### #### 
#### Appendix Descriptive Plots 

########################
## figure A3:
issues_plot <- issues_dat %>% 
  filter(!is.na(issues_dat$country_name.x) ) %>% 
  group_by(country_name.x) %>% 
  summarise(avg.hp = mean(n_hard_pledge_all),
            avg.fulfilled = mean(fulfilled_all, na.rm = T),
            avg.mentioned = mean(mentioned_all, na.rm = T), 
            avg.ment.full = mean(mentioned_and_fulfilled_all, na.rm = T)) 

issues_plot1 <- issues_plot %>% 
  select(-c(avg.mentioned, avg.ment.full)) %>% 
  pivot_longer(-c(country_name.x), values_to = "number", names_to = "type")
issues_plot1$type <- factor(issues_plot1$type, levels = c("avg.hp", "avg.fulfilled"))
plot_A3 <- ggplot(issues_plot1, aes(x = country_name.x, y = number, fill = type)) +
  geom_col(position = "dodge") + theme_bw() + labs(x = "", y= "", title = "Avg. number of commitments per country") + 
  scale_fill_manual(name = "Commitments", labels = c("Total", "Fulfilled"), values = c("firebrick3", "limegreen")) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 
plot_A3
ggsave("fig_A3_Commitments_Country.PDF")

######################## 
## Figure A4
plot_A4 <- ggplot(issues_dat, aes(y=n_hard_pledge, x=issue.x)) + 
  geom_boxplot() + theme_bw() + xlab("Issue") + ylab("Nr of commitments") + 
  #  ylim(0,100) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 
plot_A4
ggsave("fig_A4_Commitments_Issue.PDF")


########################
## Figure A5

issues_plot2 <- issues_plot %>% 
  select(-c(avg.hp, avg.fulfilled)) %>% 
  pivot_longer(-c(country_name.x), values_to = "number", names_to = "type")
issues_plot2$type <- factor(issues_plot2$type, levels = c("avg.mentioned", "avg.ment.full"))

plot_A5 <- ggplot(issues_plot2, aes(x = country_name.x, y = number, fill = type)) +
  geom_col(position = "dodge") + theme_bw() + labs(x = "", y= "", title = "Avg. number of commitments per country (mentioned and fulfilled)") + 
  scale_fill_manual(name = "Commitments", labels = c("Mentioned", "Mentioned and fulfilled"), values = c("hotpink2", "dodgerblue2")) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
plot_A5
ggsave("fig_A5_Commitments_Mentioned_Country.PDF")


########################
## Figure A6

plot_a6 <- ggplot(issues_dat, aes(y=mentioned, x=issue.x)) + 
  geom_boxplot() + theme_bw() + xlab("") + ylab("Commitments mentioned") + 
  ylim(0,50) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) 
plot_a6
ggsave("fig_A6_Commitments_Mentioned_Issues.PDF")

